package BankClientPrototype;

import javax.swing.JOptionPane;

public class ClientLogin extends javax.swing.JFrame {

    ClientHandler ch = new ClientHandler();
    int tries = 2;

    /**
     * Creates new form ClientLogin
     */
    public ClientLogin() {
        initComponents();
        this.setLocationRelativeTo(null);
    }

//    private int loginTest(String EnteredSN, String EnteredPW){
//        int valid = 0;
//        if(EnteredSN.equalsIgnoreCase(database.ClientLogin()))
//        return(valid);
//    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonLogin = new javax.swing.JButton();
        buttonRegister = new javax.swing.JButton();
        buttonExit = new javax.swing.JButton();
        labelPassword = new javax.swing.JLabel();
        labelLogin = new javax.swing.JLabel();
        textfieldLogin = new javax.swing.JTextField();
        textfieldPassword = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Log-In");

        buttonLogin.setText("Login");
        buttonLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonLoginActionPerformed(evt);
            }
        });

        buttonRegister.setText("Register");
        buttonRegister.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonRegisterActionPerformed(evt);
            }
        });

        buttonExit.setText("Exit");
        buttonExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonExitActionPerformed(evt);
            }
        });

        labelPassword.setText("Password:");

        labelLogin.setText("Login ID:");

        textfieldPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textfieldPasswordActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(buttonLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(buttonRegister)
                        .addGap(18, 18, 18)
                        .addComponent(buttonExit, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelLogin)
                            .addComponent(labelPassword))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(20, 20, 20)
                                .addComponent(textfieldLogin))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(textfieldPassword)))))
                .addContainerGap(51, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelLogin)
                    .addComponent(textfieldLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelPassword)
                    .addComponent(textfieldPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(43, 43, 43)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonLogin)
                    .addComponent(buttonRegister)
                    .addComponent(buttonExit))
                .addContainerGap(35, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonLoginActionPerformed
        if (textfieldLogin.getText().isEmpty() || textfieldPassword.getPassword() == null) {
            if (tries > 0) {
                JOptionPane.showMessageDialog(null, "Incorrect Login information. \nYou have " + tries + " attempt(s) left.", "Retry", 1);
                textfieldLogin.requestFocusInWindow();
            }
            tries--;
            if (tries < 0) {
                JOptionPane.showMessageDialog(null, "Too many failed attempts to Login. \nThe application will now exit.", "GoodBye", 2);
                System.exit(0);
            }
        } else {
            ch.fromLogin(this, textfieldLogin.getText(), textfieldPassword.getPassword(), new ClientHomepage());
        }
    }//GEN-LAST:event_buttonLoginActionPerformed

    private void buttonExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonExitActionPerformed
        System.exit(0);
    }//GEN-LAST:event_buttonExitActionPerformed

    private void buttonRegisterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRegisterActionPerformed
        ClientRegister frame = new ClientRegister();
        JOptionPane.showMessageDialog(frame, "PLEASE READ: \n Each user must have "
                + "unique usernames and passwords for each of their accounts, "
                + "checking and savings. \n You should register each account seperately, "
                + "and during registration enter 0 for the account you are not registering for. "
                + "\n Thank You.", "IMPORTANT", 2);
        frame.setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_buttonRegisterActionPerformed

    private void textfieldPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textfieldPasswordActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_textfieldPasswordActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ClientLogin().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonExit;
    private javax.swing.JButton buttonLogin;
    private javax.swing.JButton buttonRegister;
    private javax.swing.JLabel labelLogin;
    private javax.swing.JLabel labelPassword;
    private javax.swing.JTextField textfieldLogin;
    private javax.swing.JPasswordField textfieldPassword;
    // End of variables declaration//GEN-END:variables
}
